#!/bin/sh

CWD=$(pwd)
SRC=/usr/portage
DST=${CWD}/.portage
TMP="/tmp"

mkdir -p ${DST}
rm -fr ${DST}/*

find ${CWD} -mindepth 2 -maxdepth 2 -type d -printf '%P\n' > ${DST}/.portage.overlay
while read DIR ; do
    if [ ! -d ${SRC}/${DIR} ] ; then
        continue
    fi

    find ${SRC}/${DIR} -type f -newer ${CWD}/.timestamp -printf '%P\n' > ${DST}/.portage
    while read FILE ; do
        echo "# ${DIR}/${FILE}"
        SDIR=$(dirname ${DIR}/${FILE})
        mkdir -p ${DST}/${SDIR}
        cp -a ${SRC}/${DIR}/${FILE} ${DST}/${SDIR}
    done < ${DST}/.portage
    rm -f ${DST}/.portage
done < ${DST}/.portage.overlay

find ${CWD} -mindepth 3 -maxdepth 3 -type f -name '*.ebuild' -printf '%P\n' > ${DST}/.portage.overlay
while read FILE; do
    if [ ! -e ${SRC}/${FILE} ] ; then
        echo ${FILE}
    fi
done < ${DST}/.portage.overlay > ${DST}/absolete.txt

rm -f ${DST}/.portage.overlay

find ${CWD}/.portage -name 'digest-*' -exec rm -f {} \;

exit 0    
